﻿using MathNet.Numerics.Statistics;
using MediatR;
using W.Common.Enum;
using W.Warehouse.Domain;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.CheckCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.CheckCommandHandler
{
    public class CheckShowCommandHandler:IRequestHandler<CheckShowCommand,PageDTO<CheckShowDTO>>
    {
        private readonly ICheckRepository _icheckRepository;
        private readonly IStatisticsRepository _statisticsRepository;
        public CheckShowCommandHandler(ICheckRepository icheckRepository, IStatisticsRepository statisticsRepository)
        {
            _icheckRepository = icheckRepository;
            _statisticsRepository = statisticsRepository;
        }

        public async Task<PageDTO<CheckShowDTO>> Handle(CheckShowCommand request, CancellationToken cancellationToken)
        {
            PageDTO<CheckShowDTO> page = new PageDTO<CheckShowDTO>();
            var wlist = await _icheckRepository.GetAllAsync();
            var slist=await _statisticsRepository.GetAllAsync();
            var list = (from a in wlist
                        join b in slist on a.CheckId equals b.CheckId
                        where a.IsDelete == false && b.IsDelete == false
                        select new CheckShowDTO
                        {
                            CheckId = a.CheckId,
                            CheckNo = a.CheckNo,
                            CheckType = a.CheckType,
                            AssociateNumber = a.AssociateNumber,
                            CheckMakeHe = a.CheckMakeHe,
                            CheckDate = a.CheckDate,
                            CheckNotes = a.CheckNotes,
                            ProductId = a.ProductId,
                            AuditStatus = a.AuditStatus,
                            AuditStatusText = a.AuditStatus.ToString(),
                            office_id = a.office_id,
                            user_id = a.user_id,
                            ExamineReason = a.ExamineReason,
                            ExaminePeople = a.ExaminePeople,
                            ExamineTime = a.ExamineTime,
                            CreateName = a.CreateName,
                            CreateTime = a.CreateTime,
                            StatisticsId=b.StatisticsId,


                        }).DistinctBy(c=>c.CheckId). ToList();
            if (!string.IsNullOrEmpty(request.CheckNo))
            {
                list = list.Where(c => c.CheckNo == request.CheckNo).ToList();
            }
            if (request.CreateTime != null)
            {
                list = list.Where(c => c.CreateTime == request.CreateTime).ToList();
            }
            if (request.AuditStatus > 0)
            {
                list = list.Where(c => c.AuditStatus == (AuditStatus)request.AuditStatus).ToList();
            }
            page.TotalCount = list.Count();
            page.TotalPage = (int)Math.Ceiling(page.TotalCount * 1.0 / request.PageSize);
            page.List = list.OrderByDescending(c => c.CheckId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;
        }
    }
}
